
package hairmember

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/hairmember"
    hairmemberReq "github.com/flipped-aurora/gin-vue-admin/server/model/hairmember/request"
)

type ProjectTypesService struct {}
// CreateProjectTypes 创建projectTypes表记录
// Author [yourname](https://github.com/yourname)
func (projectTypesService *ProjectTypesService) CreateProjectTypes(ctx context.Context, projectTypes *hairmember.ProjectTypes) (err error) {
	err = global.GVA_DB.Create(projectTypes).Error
	return err
}

// DeleteProjectTypes 删除projectTypes表记录
// Author [yourname](https://github.com/yourname)
func (projectTypesService *ProjectTypesService)DeleteProjectTypes(ctx context.Context, id string) (err error) {
	err = global.GVA_DB.Delete(&hairmember.ProjectTypes{},"id = ?",id).Error
	return err
}

// DeleteProjectTypesByIds 批量删除projectTypes表记录
// Author [yourname](https://github.com/yourname)
func (projectTypesService *ProjectTypesService)DeleteProjectTypesByIds(ctx context.Context, ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]hairmember.ProjectTypes{},"id in ?",ids).Error
	return err
}

// UpdateProjectTypes 更新projectTypes表记录
// Author [yourname](https://github.com/yourname)
func (projectTypesService *ProjectTypesService)UpdateProjectTypes(ctx context.Context, projectTypes hairmember.ProjectTypes) (err error) {
	err = global.GVA_DB.Model(&hairmember.ProjectTypes{}).Where("id = ?",projectTypes.Id).Updates(&projectTypes).Error
	return err
}

// GetProjectTypes 根据id获取projectTypes表记录
// Author [yourname](https://github.com/yourname)
func (projectTypesService *ProjectTypesService)GetProjectTypes(ctx context.Context, id string) (projectTypes hairmember.ProjectTypes, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&projectTypes).Error
	return
}
// GetProjectTypesInfoList 分页获取projectTypes表记录
// Author [yourname](https://github.com/yourname)
func (projectTypesService *ProjectTypesService)GetProjectTypesInfoList(ctx context.Context, info hairmemberReq.ProjectTypesSearch) (list []hairmember.ProjectTypes, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&hairmember.ProjectTypes{})
    var projectTypess []hairmember.ProjectTypes
    // 如果有条件搜索 下方会自动创建搜索语句
    
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&projectTypess).Error
	return  projectTypess, total, err
}
// GetProjectTypesPublic 获取所有项目类型数据（不需要鉴权）
// Author [yourname](https://github.com/yourname)
func (projectTypesService *ProjectTypesService)GetProjectTypesPublic(ctx context.Context) (list []hairmember.ProjectTypes, err error) {
    // 查询所有状态为启用的项目类型
    err = global.GVA_DB.Where("status = ?", 1).Order("type_code asc").Find(&list).Error
    return list, err
}
